from aiogram.types import ChatMember

from app.services.db import delete, insert, select_by_keys

def insert_admins(channel_id: int, channel_title: str, admins: list[ChatMember]) -> None:
    for admin in admins:
        insert('admins', {
            "admin_id": admin.user.id,
            "channel_id": channel_id,
            "channel_title": channel_title
        })

def get_channels_by_admin_id(admin_id: int) -> list[dict]:
    return select_by_keys(
            "admins",
            ["channel_title", "channel_id"],
            {"admin_id": admin_id}
    )

def delete_admins_from_channel(channel_id: int, user_id: int = 0):
    if user_id == 0:
        delete("admins", {"channel_id": channel_id}) 
    else:
        delete("admins", {"channel_id": channel_id, "admin_id": user_id})
